﻿using DataPersistence;
using GBT_Tool;
using Newtonsoft.Json.Linq;
using NLog;
using System;
using System.Data.SqlClient;

namespace QueryDataLogic
{
    public class BasicInfo
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        ConfigManage cm = new ConfigManage();
        QueryData qd = new QueryData();
        DataPaging dp = new DataPaging();
        TimeManagement tm = new TimeManagement();

        /// <summary>
        /// 手机号码是否存在
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public bool IsMobile(string mobile, string dbName)
        {
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "SELECT id,name,mobile FROM AddressBook WHERE mobile = @mobile "
                };

                comm.Parameters.AddWithValue("@mobile", mobile);

                JArray ja = qd.GetData(comm, dbName);
                if (ja.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// 根据区域ID获取数据库信息
        /// </summary>
        /// <param name="districtID"></param>
        /// <returns></returns>
        public string GetDBName(int districtID)
        {
            string dbName = "";
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select c.DatabaseName from District d inner join Company c on d.companyID = c.ID " +
                                "where d.districtID = @districtID  "
                };

                comm.Parameters.AddWithValue("@districtID", districtID);
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    dbName = (string)ja[0]["DatabaseName"];
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
            }
            return dbName;
        }

        /// <summary>
        /// 根据公司ID获取数据库信息
        /// </summary>
        /// <param name="districtID"></param>
        /// <returns></returns>
        public string GetDBNameToCompany(int companyID)
        {
            string dbName = "";
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "SELECT DatabaseName FROM Company WHERE ID = @ID "
                };

                comm.Parameters.AddWithValue("@ID", companyID);
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    dbName = (string)ja[0]["DatabaseName"];
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
            }
            return dbName;
        }

        /// <summary>
        /// 根据邀请码获取数据库信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public string GetDBNameToCode(string code)
        {
            string dbName = "";
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select ic.code,c.DatabaseName from InvitationCode ic " + 
                        "inner join District d on ic.DistrictID = d.DistrictID " + 
                        "inner join Company c on d.CompanyID = c.ID " +
                        "where ic.code = @code "
                };

                comm.Parameters.AddWithValue("@ID", code);
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    dbName = (string)ja[0]["DatabaseName"];
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
            }
            return dbName;
        }

        /// <summary>
        /// 根据区域ID获取公司ID
        /// </summary>
        /// <param name="districtID"></param>
        /// <returns></returns>
        public int GetCompanyID(int districtID)
        {
            int companyID = 0;
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select d.companyID from District d where d.districtID = @districtID  "
                };

                comm.Parameters.AddWithValue("@districtID", districtID);
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    companyID = (int)ja[0]["companyID"];
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
            }
            return companyID;
        }

        /// <summary>
        /// 获取区域信息
        /// </summary>
        /// <returns></returns>
        public JObject GetDistrictInfo(int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                int companyID = GetCompanyID(districtID);

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select DistrictID as id,DistrictNumber as number,DistrictName as name,companyID from District " +
                    "where CompanyID = @companyID "
                };
                comm.Parameters.AddWithValue("@companyID", companyID);

                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    JArray jaDistrictTitleArray = new JArray();
                    JArray jaDistrictArray = new JArray();
                    JObject joDistrictItem = new JObject();
                    for (int districtIndex = 0; districtIndex < ja.Count; districtIndex++)
                    {
                        jaDistrictTitleArray.Add((string)ja[districtIndex]["name"]);
                        joDistrictItem = new JObject();
                        joDistrictItem = (JObject)ja[districtIndex];
                        jaDistrictArray.Add(joDistrictItem);
                    }

                    joResult.Add("districtTitleArray", jaDistrictTitleArray);
                    joResult.Add("districtArray", jaDistrictArray);
                    joResult.Add("message", "区域信息加载成功。");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "区域信息加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取餐点信息
        /// </summary>
        /// <returns></returns>
        public JObject GetMeal(int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select mealId AS id,mealNumber AS number,mealName AS name,price,familyPrice,districtID,startTime,endTime from mealTime " +
                    "where districtID = @districtID order by mealId "
                };

                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    JArray jaMealTitle = new JArray();
                    JArray jaMealTitleItem = new JArray();
                    JArray jaMeal = new JArray();
                    JArray jaMealItem = new JArray();
                    JObject joMealItem = new JObject();
                    JObject joEmpty;
                    for (int mealIndex = 0; mealIndex < ja.Count; mealIndex++)
                    {
                        jaMealTitleItem = new JArray();
                        jaMealTitleItem.Add("");
                        jaMealTitleItem.Add((string)ja[mealIndex]["name"]);
                        jaMealTitle.Add(jaMealTitleItem);

                        jaMealItem = new JArray();
                        joMealItem = new JObject();
                        joEmpty = new JObject();
                        joEmpty["id"] = 0;
                        joEmpty["mealId"] = ja[mealIndex]["id"];
                        joEmpty["number"] = ja[mealIndex]["number"];
                        joEmpty["name"] = ja[mealIndex]["name"];
                        joEmpty["qty"] = 0;
                        joEmpty["price"] = ja[mealIndex]["price"];
                        joEmpty["districtID"] = ja[mealIndex]["districtID"];
                        joEmpty["startTime"] = ja[mealIndex]["startTime"];
                        joEmpty["endTime"] = ja[mealIndex]["endTime"];

                        joMealItem["id"] = 1;
                        joMealItem["mealId"] = ja[mealIndex]["id"];
                        joMealItem["number"] = ja[mealIndex]["number"];
                        joMealItem["name"] = ja[mealIndex]["name"];
                        joMealItem["qty"] = 1;
                        joMealItem["price"] = ja[mealIndex]["price"];
                        joMealItem["districtID"] = ja[mealIndex]["districtID"];
                        joMealItem["startTime"] = ja[mealIndex]["startTime"];
                        joMealItem["endTime"] = ja[mealIndex]["endTime"];
                        jaMealItem.Add(joEmpty);
                        jaMealItem.Add(joMealItem);
                        jaMeal.Add(jaMealItem);
                    }

                    //logger.Info(jaMeal);
                    joResult.Add("mealTitleArray", jaMealTitle);
                    joResult.Add("mealObjectArray", jaMeal);
                    joResult.Add("message", "餐点信息加载成功。");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", "");
                    joResult.Add("message", "餐点信息加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", "");
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取餐点信息 列表（单选）
        /// </summary>
        /// <returns></returns>
        public JObject GetMealList(int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select mealId AS id,mealNumber AS number,mealName AS name,price,familyPrice,districtID,startTime,endTime from mealTime " +
                    "where districtID = @districtID order by mealId"
                };
                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    //logger.Info(ja);
                    joResult.Add("data", ja);
                    joResult.Add("message", "餐点信息加载成功。");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "餐点信息加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取食堂信息
        /// </summary>
        /// <returns></returns>
        public JObject GetDiningHall(int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select h.hallid AS id,h.hallNumber AS number,h.hallName as name,h.districtID,h.deadline from dininghall h " +
                    "where h.districtID = @districtID "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);
                
                if (ja.Count > 0)
                {
                    JArray jaDiningHallTitleArray = new JArray();
                    JArray jaDiningHallArray = new JArray();
                    JObject joDiningHallItem = new JObject();
                    for (int diningHallIndex = 0; diningHallIndex < ja.Count; diningHallIndex++)
                    {
                        jaDiningHallTitleArray.Add((string)ja[diningHallIndex]["name"]);

                        joDiningHallItem = new JObject();
                        joDiningHallItem = (JObject)ja[diningHallIndex];
                        jaDiningHallArray.Add(joDiningHallItem);
                    }

                    joResult.Add("diningHallTitleArray", jaDiningHallTitleArray);
                    joResult.Add("diningHallArray", jaDiningHallArray);
                    joResult.Add("message", "食堂信息加载成功。");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "食堂信息加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取通讯录信息
        /// </summary>
        /// <returns></returns>
        public JObject GetAddressBook(int CurrentPage,int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select id,name,mobile,districtID from AddressBook " +
                    "where districtID = @districtID and mobile not like '9%' order by id "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "通讯录加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取评价项目
        /// </summary>
        /// <returns></returns>
        public JObject GetEvaluation(int CurrentPage, int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select id,name,number,districtID,remark,isEnable from EvaluationContent " +
                    "where districtID = @districtID order by id "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "评价项目加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取区域信息
        /// </summary>
        /// <returns></returns>
        public JObject GetDistrict(int CurrentPage, string name)
        {
            JObject joResult = new JObject();
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select District.DistrictID AS id, District.DistrictNumber AS number, District.DistrictName AS name ," +
                    "ic.code, District.companyID,c.Name as companyName from District " +
                    "inner join InvitationCode ic on District.DistrictID = ic.DistrictID " +
                    "inner join Company c on District.CompanyID = c.ID " +
                    "WHERE District.DistrictName like @name " +
                    "order by c.ID, District.DistrictID "
                };
                comm.Parameters.AddWithValue("@name", "%" + name + "%");
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "区域加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取区域信息（当前公司）
        /// </summary>
        /// <returns></returns>
        public JObject GetDistrictCurrent(int CurrentPage, int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                int companyID = GetCompanyID(districtID);
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select District.DistrictID AS id, District.DistrictNumber AS number, District.DistrictName AS name ," +
                    "ic.code, District.companyID,c.Name as companyName from District " +
                    "inner join InvitationCode ic on District.DistrictID = ic.DistrictID " +
                    "inner join Company c on District.CompanyID = c.ID " +
                    "WHERE District.companyID = @companyID " +
                    "order by c.ID, District.DistrictID "
                };
                comm.Parameters.AddWithValue("@companyID", companyID);
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "区域加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取公司信息
        /// </summary>
        /// <returns></returns>
        public JObject GetCompany(int CurrentPage, string name)
        {
            JObject joResult = new JObject();
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "SELECT id,number,name,address,mobile,tel,legal_representative,reg_num,isAudit,remark FROM Company WHERE name like @name "
                };
                comm.Parameters.AddWithValue("@name", "%" + name + "%");
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "公司信息加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取菜品分类信息
        /// </summary>
        /// <returns></returns>
        public JObject GetDishesClass(int CurrentPage, int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select dc.id,dc.number,dc.name,dc.districtID,dc.hallID, dh.hallNumber, dh.hallName,dc.remark,dc.qty from DishesClass dc " +
                    "inner join DiningHall dh on dc.hallID = dh.hallID " +
                    "where dc.districtID = @districtID order by dc.number "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "菜品分类加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取菜品分类信息(picker)
        /// </summary>
        /// <returns></returns>
        public JObject GetDishesClassPicker(int hallID, int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select dc.id,dc.number,dc.name,dc.districtID,dc.hallID, dh.hallNumber, dh.hallName,dc.remark,dc.qty from DishesClass dc " +
                    "inner join DiningHall dh on dc.hallID = dh.hallID " +
                    "where dc.districtID = @districtID and dc.hallID = @hallID order by dc.number "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);
                comm.Parameters.AddWithValue("@hallID", hallID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    joResult.Add("data", ja);
                    joResult.Add("message", "菜品分类加载成功。");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "菜品分类加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取属性信息（picker）
        /// </summary>
        /// <returns></returns>
        public JObject GetAttributePicker(int hallID, int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";
                dbName = GetDBName(districtID);

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select a.id,a.number,a.name,a.type,a.districtID,a.hallID,dh.hallNumber,dh.hallName,a.remark from Attribute a " +
                    "inner join DiningHall dh on dh.hallID = a.hallID " +
                    "where a.hallID = @hallID and a.districtID = @districtID order by a.hallID "
                };
                comm.Parameters.AddWithValue("@hallID", hallID);
                comm.Parameters.AddWithValue("@districtID", districtID);
                
                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    JArray jaArributeTitleArray = new JArray();
                    JArray jaArributeArray = new JArray();
                    JObject joArributeItem = new JObject();
                    for (int arributeIndex = 0; arributeIndex < ja.Count; arributeIndex++)
                    {
                        jaArributeTitleArray.Add((string)ja[arributeIndex]["name"]);

                        joArributeItem = new JObject();
                        joArributeItem = (JObject)ja[arributeIndex];
                        jaArributeArray.Add(joArributeItem);
                    }

                    joResult.Add("arributeTitleArray", jaArributeTitleArray);
                    joResult.Add("arributeArray", jaArributeArray);
                    joResult.Add("message", "属性信息加载成功。");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "属性信息加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取属性信息
        /// </summary>
        /// <returns></returns>
        public JObject GetAttribute(int CurrentPage, int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select a.id,a.number,a.name,a.type,a.districtID,a.hallID,dh.hallNumber,dh.hallName,a.remark from Attribute a " +
                    "inner join DiningHall dh on dh.hallID = a.hallID " +
                    "where a.districtID = @districtID order by a.hallID "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "属性加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取菜品信息(菜品设置使用)
        /// </summary>
        /// <returns></returns>
        public JObject GetDishesSet(JObject joParam)
        {
            JObject joResult = new JObject();
            try
            {
                int CurrentPage = (int)joParam["currentPage"];
                int districtID = (int)joParam["districtID"];
                int hallID = (int)joParam["hallID"];
                string attributeID = (string)joParam["attributeID"];
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select d.id,d.dishesClassID,d.number,d.name,d.imgUrl,d.remark,d.isEnable,dh.hallId,dh.hallNumber,dh.hallName,dh.deadLine, " +
                    "dc.Number as dishesClassNumber,dc.name as dishesClassName,dc.qty,da.name as dishesAttributeName,a.number as attributeNumber, " +
                    "a.name as attributeName from Dishes d inner join DiningHall dh on d.HallID = dh.HallId and d.districtID = dh.DistrictID " +
                    "inner join DishesClass dc on d.DishesClassID = dc.ID and d.districtID = dc.districtID and d.hallID = dc.hallID " +
                    "left join DishesAttribute da on da.DishesID = d.ID and d.districtID = da.districtID and d.hallID = da.hallID " +
                    "left join Attribute a on da.AttributeID = a.ID and d.districtID = a.districtID and d.hallID = a.hallID and a.[type] = 0 " +
                    "where d.districtID = @districtID and d.hallID = @hallID and ISNULL(a.id,'') like @attributeID " +
                    "order by dc.number,d.id,a.id "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);
                comm.Parameters.AddWithValue("@hallID", hallID);
                comm.Parameters.AddWithValue("@attributeID", "%" + attributeID + "%");

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);
                JObject item = new JObject();
                JArray jaDishes = new JArray();
                JObject joDishes = new JObject();
                int dishesID = 0;
                int newDishesIndex = 0;
                string attributeName = "";
                
                if (ja.Count > 0)
                {
                    for (int dishesIndex = 0; dishesIndex < ja.Count; dishesIndex++)
                    {
                        item = (JObject)ja[dishesIndex];
                        joDishes = new JObject();
                        if (dishesID != (int)item["id"])
                        {
                            newDishesIndex++;
                            attributeName = (string)item["attributeName"] + " ";
                            joDishes.Add("id", (int)item["id"]);
                            joDishes.Add("number", (string)item["number"]);
                            joDishes.Add("name", (string)item["name"]);
                            joDishes.Add("dishesClassID", (int)item["dishesClassID"]);
                            joDishes.Add("dishesClassName", (string)item["dishesClassName"]);
                            joDishes.Add("imgUrl", (string)item["imgUrl"]);
                            joDishes.Add("attributeName", attributeName.Trim());
                            joDishes.Add("isEnable", (bool)item["isEnable"]);
                            jaDishes.Add(joDishes);
                            dishesID = (int)item["id"];
                        }
                        else
                        {
                            attributeName += (string)item["attributeName"] + " ";
                            jaDishes[newDishesIndex - 1]["attributeName"] = attributeName.Trim();
                        }
                    }

                    joResult = dp.PagingProcessing(CurrentPage, jaDishes);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "菜品加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取菜品信息 详情页
        /// </summary>
        /// <returns></returns>
        public JObject GetDishesDetail(int districtID, int id)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select d.id,d.dishesClassID,d.number,d.name,d.imgUrl,d.remark,d.districtID,d.isEnable,dh.hallID,dh.hallNumber,dh.hallName,dh.deadLine, " +
                    "dc.Number as dishesClassNumber,dc.name as dishesClassName,dc.qty,ISNULL(da.name,'') as dishesAttributeName," +
                    "ISNULL(a.id,0) as attributeID,ISNULL(a.number,'') as attributeNumber, " +
                    "ISNULL(a.name,'') as attributeName,ISNULL(da.remark,'') as dishesAttributeRemark,ISNULL(a.[type],0) as [type],ISNULL(da.id,0) as dishesAttributeID " +
                    "from Dishes d inner join DiningHall dh on d.HallID = dh.HallId and d.districtID = dh.DistrictID " +
                    "inner join DishesClass dc on d.DishesClassID = dc.ID and d.districtID = dc.districtID and d.hallID = dc.hallID " +
                    "left join DishesAttribute da on da.DishesID = d.ID and d.districtID = da.districtID and d.hallID = da.hallID " +
                    "left join Attribute a on da.AttributeID = a.ID and d.districtID = a.districtID and d.hallID = a.hallID and a.[type] = 0 " +
                    "where d.id = @id and d.districtID = @districtID " +
                    "order by dc.id,a.id  "
                };
                comm.Parameters.AddWithValue("@id", id);
                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);
                JObject item = new JObject();
                JObject joDishes = new JObject(); 
                JObject joAttribute = new JObject();
                JArray jaAttribute = new JArray();
                string attributeName = "";
                int dishesID = 0;

                if (ja.Count > 0)
                {
                    for (int index = 0; index < ja.Count; index++)
                    {
                        item = (JObject)ja[index];
                        if ((int)item["dishesAttributeID"] != 0)
                        {
                            joAttribute = new JObject();
                            joAttribute["id"] = (int)item["dishesAttributeID"];//菜品属性ID
                            joAttribute["attributeID"] = (int)item["attributeID"];//属性ID 
                            joAttribute["hallID"] = (int)item["hallID"];
                            joAttribute["number"] = (string)item["attributeNumber"];
                            joAttribute["name"] = (string)item["attributeName"];
                            joAttribute["type"] = (int)item["type"];
                            joAttribute["remark"] = (string)item["dishesAttributeRemark"];
                            joAttribute["districtID"] = (int)item["districtID"];
                            jaAttribute.Add(joAttribute);
                        }
                        if (dishesID != (int)item["id"])
                        {
                            attributeName = (string)item["attributeName"] + " ";
                            joDishes["id"] = (int)item["id"];
                            joDishes["number"] = (string)item["number"];
                            joDishes["name"] = (string)item["name"];
                            joDishes["imgUrl"] = (string)item["imgUrl"];
                            joDishes["districtID"] = (int)item["districtID"];
                            joDishes["dishesClassID"] = (int)item["dishesClassID"];
                            joDishes["dishesClassNumber"] = (string)item["dishesClassNumber"];
                            joDishes["dishesClassName"] = (string)item["dishesClassName"];
                            joDishes["hallID"] = (int)item["hallID"];
                            joDishes["hallNumber"] = (string)item["hallNumber"];
                            joDishes["hallName"] = (string)item["hallName"];
                            joDishes["remark"] = (string)item["remark"];
                            joDishes["isEnable"] = (bool)item["isEnable"];
                            joDishes["attributeNameGroup"] = attributeName.Trim();
                            dishesID = (int)item["id"];
                        }
                        else
                        {
                            if ((int)item["dishesAttributeID"] != 0)
                            {
                                attributeName += (string)item["attributeName"] + " ";
                                joDishes["attributeNameGroup"] = attributeName.Trim();
                            }
                        }
                    }
                    joDishes["attributeList"] = jaAttribute;

                    joResult.Add("data", joDishes);
                    joResult.Add("message", "菜品加载成功");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "菜品加载失败");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }
    }
}
